package com.sgj.readwrite;

import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock.*;

/**
 * @Author: Guoji Shen
 * @Date: 2021/8/10 13:56
 */
public class Demo01 {
    public static void main(String[] args) {
        // 可重入读写锁对象
        ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
        // 读锁
        ReadLock readLock = rwLock.readLock();
        // 写锁
        WriteLock writeLock = rwLock.writeLock();

        // 锁降级
        writeLock.lock();
        System.out.println("write");

        // 获取读锁
        readLock.lock();
        System.out.println("read");

        // 释放写锁
        writeLock.unlock();

        // 释放读锁
        readLock.unlock();
    }
}